from sqlalchemy import Column, Integer, String, ForeignKey
from sqlalchemy.orm import relationship

from models import Base

class Student(Base):
    """
        学生信息
    """
    __tablename__ = "student"
    # __table_args__ = {'mysql_collate': 'utf8mb4_unicode_ci'}  # 设置字符集和校对规则
    id = Column(Integer, primary_key=True,autoincrement=True)
    sno = Column(String(8),unique=True, index=True,comment="学号")
    name = Column(String(10),index=True,comment="姓名")
    grade = Column(String(10),comment="班级")
    email = Column(String(50),unique=True,comment="邮箱")

    courses=relationship("Course",secondary="course_student",back_populates="students",lazy="dynamic")

class StudentExtension(Base):
    """
        学生扩展信息
    """
    __tablename__ = "student_extension"
    id = Column(Integer, primary_key=True,autoincrement=True)
    sex = Column(Integer,index=True,default=1,comment="1 男，2 女")
    addr = Column(String(20), comment="地址")

    student_id=Column(Integer, ForeignKey("student.id"))
    # 使用uselist=False,表示和Student建立一对一的关系
    student=relationship('Student',backref="student_extension",uselist=False)
